package cn.wowjoy.pda.android_pharmacy.common.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGABindingRecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGABindingViewHolder;
import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.databinding.ItemDialogListBinding;

import static cn.wowjoy.pda.android_pharmacy.R.id.alert_dialog_cancel_btn;
import static cn.wowjoy.pda.android_pharmacy.R.id.alert_dialog_confirm_btn;
import static cn.wowjoy.pda.android_pharmacy.R.id.alert_dialog_message;
import static cn.wowjoy.pda.android_pharmacy.R.id.alert_dialog_title;
import static cn.wowjoy.pda.android_pharmacy.R.id.imageView;

public class MDialog extends Dialog {
    private int img;
    private String msg;
    private ImageView imgIV;
    private TextView msgTV;
    private boolean isRoll;
    private boolean ref;
    private String mTitle = null;
    private String mHintStr = null;
    private String mBottonL = null;
    private String mBottonR = null;
    private TextView mTileTextView;
    private TextView mHintTextView;
    private View.OnClickListener mCancleClickListener = null;
    private View.OnClickListener mConfirmClickListener = null;
    private int mType = 0;
    private Button mCancelBtn = null;
    private Button mConfirmBtn = null;
    private List<String> data;
    private ViewDataBinding vb;
    private LRecyclerView mListRV;
    private LayoutInflater inflater;
    private Activity context;
    private int check;
    public BGABindingRecyclerViewAdapter<String, ItemDialogListBinding> areainnerAdapter;
    public LRecyclerViewAdapter areaAdapter;
    public OnListClickedInterface mACInterface;

    public MDialog(Activity context, int theme) {
        super(context, theme);
    }
    public MDialog(Activity context, int theme ,boolean ref) {
        super(context, theme);
          this.ref=ref;
    }
    public MDialog(Activity context, int theme ,int check ) {
        super(context, theme);
        this.check=check;
    }

    public MDialog(Activity context, int theme ,int check , String title) {
        super(context, theme);
        this.check=check;
        this.mTitle=title;
    }
    public MDialog(Activity context, int theme , int check , String title, String hintStr) {
        super(context, theme);
        this.check=check;
        this.mTitle=title;
        this.mHintStr=hintStr;
    }
    public MDialog(Activity context, int theme , int check , String title, String hintStr, View.OnClickListener confirmClickListener) {
        super(context, theme);
        this.check=check;
        this.mTitle=title;
        this.mHintStr=hintStr;
        this.mConfirmClickListener= confirmClickListener ;
    }
    public MDialog(Activity context, int theme ,int check , View.OnClickListener confirmBtnClickListener ,String hint) {
        super(context, theme);
        this.check=check;
        mConfirmClickListener = confirmBtnClickListener;
        this.mHintStr=hint;
    }
    public MDialog(Activity context, int theme, String title, String hint, String BottonL, String BottonR, View.OnClickListener cancelBtnClickListener, View.OnClickListener confirmBtnClickListener) {
        super(context, theme);
        mTitle = title;
        mHintStr = hint;
        mBottonL = BottonL;
        mBottonR = BottonR;
        mCancleClickListener = cancelBtnClickListener;
        mConfirmClickListener = confirmBtnClickListener;
    }

    public MDialog(Activity context, int theme, String hint, String BottonL, String BottonR, View.OnClickListener cancelBtnClickListener, View.OnClickListener confirmBtnClickListener) {
        super(context, theme);
        mHintStr = hint;
        mBottonL = BottonL;
        mBottonR = BottonR;
        mCancleClickListener = cancelBtnClickListener;
        mConfirmClickListener = confirmBtnClickListener;
    }

    public MDialog(Activity context, int theme, String title, int type) {
        super(context, theme);
        mTitle = title;
        mType = type;
        this.context = context;
    }
//    public MDialog(Activity context, int theme, String title ,boolean ref) {
//        super(context, theme);
//        mTitle = title;
//        mType = type;
//        this.ref=ref;
//        this.context = context;
//    }

    public MDialog(Activity context, int theme, List<String> data) {
        super(context, theme);
        this.data = data;
        this.context = context;
        if (inflater == null)
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public MDialog(Activity context, int theme, int img, String msg, boolean anmi) {
        super(context, theme);
        this.context = context;
        this.img = img;
        this.msg = msg;
        this.isRoll = anmi;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (img != 0) {
            setContentView(R.layout.dialog_muti);
            imgIV = (ImageView) findViewById(R.id.alert_dialog_img);
            msgTV = (TextView) findViewById(alert_dialog_message);
            imgIV.setImageResource(img);
            msgTV.setText(msg);
            if (isRoll) {
                Animation animation = AnimationUtils.loadAnimation(context, R.anim.animate_waiting_progress);
                animation.setInterpolator(new LinearInterpolator());
                imgIV.startAnimation(animation);
            }
        }
//        else if (TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mHintStr)) {
//            setContentView(R.layout.dialog_alert);
//            mHintTextView = (TextView) findViewById(alert_dialog_message);
//            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
//            mConfirmBtn.setOnClickListener(mConfirmClickListener);
//            mCancelBtn = (Button) findViewById(alert_dialog_cancel_btn);
//            mCancelBtn.setOnClickListener(mCancleClickListener);
//
//            mHintTextView.setText(mHintStr);
//            mCancelBtn.setText(mBottonL);
//            mConfirmBtn.setText(mBottonR);
//        }
//        else if (!TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mHintStr)) {
//            setContentView(R.layout.dialog_update);
//            mTileTextView = (TextView) findViewById(alert_dialog_title);
//            mHintTextView = (TextView) findViewById(alert_dialog_message);
//            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
//            mConfirmBtn.setOnClickListener(mConfirmClickListener);
//            mCancelBtn = (Button) findViewById(alert_dialog_cancel_btn);
//            mCancelBtn.setOnClickListener(mCancleClickListener);
//
//            mTileTextView.setText(mTitle);
//            mHintTextView.setText(mHintStr);
//            mCancelBtn.setText(mBottonL);
//            mConfirmBtn.setText(mBottonR);
//            if (TextUtils.isEmpty(mBottonL))
//                mCancelBtn.setVisibility(View.GONE);
//            if (TextUtils.isEmpty(mBottonR))
//                mConfirmBtn.setVisibility(View.GONE);
//        }
        else if (data != null) {
            vb = DataBindingUtil.inflate(inflater, R.layout.dialog_list, null, false);
            setContentView(vb.getRoot());
            areainnerAdapter = new BGABindingRecyclerViewAdapter<>(R.layout.item_dialog_list);
            areainnerAdapter.setData(data);
            areainnerAdapter.setItemEventHandler(this);
            areaAdapter = new LRecyclerViewAdapter(areainnerAdapter);
            mListRV = (LRecyclerView) vb.getRoot().findViewById(R.id.rv);
            mListRV.setLayoutManager(new LinearLayoutManager(context));
            mListRV.setPullRefreshEnabled(false);
            mListRV.setAdapter(areaAdapter);
            areaAdapter.removeHeaderView();
            areaAdapter.removeFooterView();
        } else if(check == 1){
            setContentView(R.layout.dialog_success2);
            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
            mConfirmBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });

        }else if( TextUtils.isEmpty(mHintStr) && ref){
            setContentView(R.layout.dialog_success);

        }else if(check == 6 && mTitle != null){
            setContentView(R.layout.dialog_success3);
            mTileTextView = (TextView) findViewById(alert_dialog_message);
            mTileTextView.setText(mTitle);

        }else if(check == 99 && mTitle != null){
            setContentView(R.layout.dialog_success3);
            mTileTextView = (TextView) findViewById(alert_dialog_message);
            imgIV= (ImageView) findViewById(imageView);
            imgIV.setBackgroundResource(R.mipmap.repeat_audit);
            mTileTextView.setText(mTitle);

        }
//        else if (!TextUtils.isEmpty(mTitle) && TextUtils.isEmpty(mHintStr) && mConfirmClickListener == null) {
//            setContentView(R.layout.dialog_info);
//            mTileTextView = (TextView) findViewById(alert_dialog_message);
//            if (mType != 0)
//                mTileTextView.setTextColor(context.getResources().getColor(R.color.text_color_alert));
//
//            mTileTextView.setText(mTitle);
//        }
        else if(check == 2 && mTitle != null){
            setContentView(R.layout.dialog_false);
            mHintTextView= (TextView) findViewById(alert_dialog_message);
            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
            mHintTextView.setText(mTitle);
            mConfirmBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CreateDialog.dismiss(context);
                    cancel();
                }
            });
        }  else if(check == 2 ){
            setContentView(R.layout.dialog_false);
//            mHintTextView= (TextView) findViewById(alert_dialog_message);
            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
//            mHintTextView.setText(mTitle);
            mConfirmBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CreateDialog.dismiss(context);
                    cancel();
                }
            });
        }else if(check == 3){
            setContentView(R.layout.dialog_false2);
            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
            mConfirmBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });
        }else if(check == 4  && mTitle !=null && mHintStr != null){
            setContentView(R.layout.dialog_false2);
            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
            mTileTextView= (TextView) findViewById(alert_dialog_title);
            mHintTextView = (TextView) findViewById(alert_dialog_message);
            mTileTextView.setText(mTitle);
            mHintTextView.setText(mHintStr);
            mConfirmBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });
        }else if(check == 5 && mConfirmClickListener != null && mHintStr != null){
            setContentView(R.layout.dialog_exit);
            mHintTextView= (TextView) findViewById(alert_dialog_message);
            mHintTextView.setText(mHintStr);
            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
            mConfirmBtn.setOnClickListener(mConfirmClickListener);
            mCancelBtn= (Button) findViewById(alert_dialog_cancel_btn);
            mCancelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });
        }else if(check == 7 && mConfirmClickListener != null && mTitle !=null && mHintStr != null ){
            setContentView(R.layout.dialog_false3);
            mTileTextView= (TextView) findViewById(alert_dialog_title);
            mHintTextView= (TextView) findViewById(alert_dialog_message);
            mTileTextView.setText(mTitle);
            mHintTextView.setText(mHintStr);
            mConfirmBtn = (Button) findViewById(alert_dialog_confirm_btn);
            mConfirmBtn.setOnClickListener(mConfirmClickListener);

        }else if (TextUtils.isEmpty(mHintStr))
            setContentView(R.layout.dialog_waiting);
    }

    public void OnListItemClicked(BGABindingViewHolder holder, String model) {
        if (mACInterface != null)
            mACInterface.OnListClicked(model);
        dismiss();
    }

    public void OnListClickItem(OnListClickedInterface onListClickedInterface) {
        mACInterface = onListClickedInterface;
    }


    public interface OnListClickedInterface {
        void OnListClicked(String ai);
    }

    public void setRes(int res, String msg) {
        if (imgIV != null) {
            imgIV.clearAnimation();
            imgIV.setImageResource(res);
        }
        if (msgTV != null)
            msgTV.setText(msg);
    }
}
